from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
from PyQt5.uic import loadUiType
import sys
import Main,dbutil

cp, _ = loadUiType('changepassword.ui')
#修改密码界面
class Change_passwordApp(QMainWindow,cp):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.confirmchangePushButton.clicked.connect(lambda:self.edit_users())
        self.menu.triggered[QAction].connect(self.menuTriggered)


    def menuTriggered(self, q):
        if (q.text() == "返回上一页"):
            self.mainwindow_app = Main.LoginApp()
            self.close()
            self.mainwindow_app.show()

        if (q.text() == "退出"):
            app = QApplication.instance()
            app.quit()
        return


# 修改密码
    def edit_users(self):
        oldpassword = self.oldpasswordLineEdit.text()
        num = self.accountnumberLineEdit.text()
        newpassword = self.newpasswordLineEdit.text()
        confirmpassword = self.confirmLineEdit.text()

        if (num == "" or oldpassword == "" or newpassword == "" or confirmpassword == ""):
            print(QMessageBox.warning(self, "警告", "输入不可为空", QMessageBox.Yes))
            return
        # 获取连接
        conn = dbutil.get_conn()
        # 获取cursor
        cur = conn.cursor()
        # SQL语句
        sql = "select * from users where id=%s"
        # 执行语句
        cur.execute(sql, (num,))
        # insert,update,delete必须显示提交

        # 关闭资源
        #dbutil.close_conn(conn, cur)
        data =cur.fetchone()
        if not data:
            print(QMessageBox.warning(self, "警告", "该账号不存在",QMessageBox.Yes))
            self.accountnumberLineEdit.clear()
            self.statusBar().showMessage('请重新输入账号')
            return
        # 获取连接
        conn = dbutil.get_conn()
        # 获取cursor
        cur = conn.cursor()
        # SQL语句
        sql1 = "select * from users where id=%s and password=%s"
        # 执行语句
        cur.execute(sql1, (num,oldpassword))
        # insert,update,delete必须显示提交
        #conn.commit()
        # 关闭资源
        #dbutil.close_conn(conn, cur)
        data1=cur.fetchone()
        if not data1:
            print(QMessageBox.warning(self,"警告", "旧密码输入错误", QMessageBox.Yes))
            self.statusBar().showMessage('请重新输入旧密码')
            self.oldpasswordLineEdit.clear()
            return
        if (confirmpassword != newpassword):
            print(QMessageBox.warning(self, "警告", "两次输入密码不同",QMessageBox.Yes))
            self.statusBar().showMessage('请重新输入新密码')
            self.newpasswordLineEdit.clear()
            self.confirmLineEdit.clear()
            return
        # 获取连接
        conn = dbutil.get_conn()
        # 获取cursor
        cur = conn.cursor()
        # SQL语句
        sql2 = "update users set password=%s where id=%s"
        # 执行语句
        cur.execute(sql2, (newpassword,num))
        # insert,update,delete必须显示提交
        conn.commit()
        # 关闭资源
        dbutil.close_conn(conn, cur)
        QMessageBox.information(self, "提示", "修改密码成功!", QMessageBox.Yes)
        # 消息提示
        self.confirmLineEdit.setText('')
        self.newpasswordLineEdit.setText('')
        self.oldpasswordLineEdit.setText('')
        self.accountnumberLineEdit.setText('')
        return





def main():
    app = QApplication([])     # 创建应用程序 创建一个QApplication，也就是你要开发的软件app
    window = Change_passwordApp()         # 创建主窗口  创建一个MainAPP，用来装载你需要的各种组件、控件
    window.show()              # 显示窗口 执行QMainWindow的show()方法，显示这个QMainWindow
    app.exec_()                # 使用exit()或者点击关闭按钮退出QApplication


if __name__ == '__main__':
    main()